package netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.sctp.nio.NioSctpServerChannel;
import io.netty.channel.socket.SocketChannel;

import java.net.InetSocketAddress;

/**
 * create by yongli on 2020-07-31 23:05
 * 这个异步的回调实战
 * 这里又引出了很多其他概念：EventLoopGroup, 当然Channel是最基本的概念
 * 通过概念图来理解netty框架，可能会比较好点
 */

public class EchoServer {
    public static void main(String[] args) {
        EchoServerChannelHandler handler = new EchoServerChannelHandler();
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(group)
                    .channel(NioSctpServerChannel.class)
                    .localAddress(new InetSocketAddress(6789))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(handler);
                        }
                    });

            ChannelFuture channelFuture = b.bind().sync();
            channelFuture.channel().closeFuture().sync(); // 后面是要求阻塞

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
